﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using System;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [AppService]
    public class ArticleMarkService
    {
        private readonly ArticleMarkRepository  _articleMarkRepository;
        private readonly ArticleRepository  _articleRepository;

        public ArticleMarkService(ArticleMarkRepository  articleMarkRepository, ArticleRepository articleRepository)
        {
            _articleMarkRepository = articleMarkRepository;
            _articleRepository = articleRepository;
        }


        public async Task<ViewArticleMark> GetOneAsync(int aid)
        {
            ViewArticleMark artMark =await _articleMarkRepository.GetOneAsync(aid);
            if(artMark == null)
            {
                ViewArticleMark am = new();
                ViewArticleAndCate article =await _articleRepository.GetOneForMarkAsync(aid);
                am.Title = article.Title;
                am.Aid = article.Id;
                am.Nickname = article.Nickname;
                am.InsertTime = article.InsertTime;
                return am;
            }
            else
            {
                return artMark;
            }
        }

        public async Task<int> InsertOrUpdateAsync(EditArticleMark  articleMark)
        {
            ArticleMark mark = new()
            {
                Id = articleMark.Id,
                Aid = articleMark.Aid,
                Uid = articleMark.Uid,
                Status = 1,
                AdoptAppraise = articleMark.AdoptAppraise,
                Remark = articleMark.Remark,
            };
            return await _articleMarkRepository.InsertOrUpdateAsync(mark);
        }

        public async Task<int> DeleteByPriKeyAsync(int priKey)
        {
            return await _articleMarkRepository.DeleteByPriKeyAsync(priKey);
        }
    }
}
